<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../static/static/css/font.css">
    <link rel="stylesheet" href="../static/static/css/weadmin.css">
    <script src="../static/lib/layui/layui.js" charset="utf-8"></script>
</head>
<body>
<br>
<fieldset class="layui-elem-field">
    <legend>商品新增</legend>
    <div class="layui-field-box">
        以下表单为商品的基础信息
    </div>
</fieldset>
<br><br>
<form class="layui-form" onsubmit="return false;">
    <div class="layui-form-item">
        <label class="layui-form-label">商品标题</label>
        <div class="layui-input-block">
            <input type="text" id="name" name="name" autocomplete="off" placeholder="请输入标题"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">商品类别</label>
            <div class="layui-input-block">
                <select id="type" name="type">

                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block">
                <select id="status" name="status">
                    <option value="上架">上架</option>
                    <option value="下架">下架</option>
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">编号</label>
            <div class="layui-input-block">
                <input type="text" id="code" name="code" autocomplete="off" placeholder="请输入编号"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">一口价</label>
            <div class="layui-input-block">
                <input type="text" id="price" name="price" autocomplete="off" placeholder="请输入一口价"
                       class="layui-input">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-upload">
            <div class="layui-upload-list">
                <table class="layui-table">
                    <thead>
                    <tr>
                        <th>文件名</th>
                        <th>大小</th>
                        <th>状态</th>
                        <th>图片路径</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody id="imgMainList"></tbody>
                </table>
            </div>
            <button type="button" class="layui-btn" id="mainAction">开始上传</button>
            <button type="button" class="layui-btn layui-btn-normal" id="choseMain">选择主图</button>
            <button class="layui-btn layui-btn-normal" onclick="addMainTB()">添加第三方链接</button>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-upload">
            <div class="layui-upload-list">
                <table class="layui-table">
                    <thead>
                    <tr>
                        <th>文件名</th>
                        <th>大小</th>
                        <th>状态</th>
                        <th>存储路径</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody id="imgDetailList"></tbody>
                </table>
            </div>
            <button type="button" class="layui-btn" id="detailAction">开始上传</button>
            <button type="button" class="layui-btn layui-btn-normal" id="choseDetail">选择详情页</button>
            <button class="layui-btn layui-btn-normal" onclick="addDetailTB()">添加第三方链接</button>
        </div>
    </div>
    <fieldset class="layui-elem-field layui-field-title">
        <legend>以下部分为款式</legend>
    </fieldset>
    <div id="modelList">

    </div>
    <br><br><br>
    <div class="layui-form-item" style="text-align: center">
        <div class="layui-inline">
            <button class="layui-btn layui-btn-fluid" onclick="addStyleModel()">添加款式</button>
        </div>
        <div class="layui-inline">
            <button class="layui-btn layui-btn-warm" onclick="addGoods()">提交</button>
        </div>
    </div>
    <br>
</form>
<script>
    var index = 0;
    var tbIndex = 0;
    layui.use(['form', 'table', 'upload'], function () {
        var $ = layui.$;
        // 初始化内容
        addStyleModel();
        var form = layui.form;
        $.ajax({
            type: 'GET',
            url: '/goodsType/getTypeSelect',
            dataType: 'json',
            success: function (data) {
                var typeList = data.data;
                for (var i = 0; i < typeList.length; i++) {
                    var option1 = $("<option>").val(typeList[i].id).text(typeList[i].name);
                    $("#type").append(option1);
                }
                form.render('select');
                $("#type").get(0).selectedIndex = 0;
            }
        });
        var imgMainListView = $('#imgMainList')
                , uploadListMain = layui.upload.render({
            elem: '#choseMain'
            , url: '/util/uploadImg'
            , data: {
                path: 'goods/main',
                type: 'reduce'
            }
            , accept: 'file'
            , multiple: true
            , auto: false
            , bindAction: '#mainAction'
            , choose: function (obj) {
                var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列
                //读取本地文件
                obj.preview(function (index, file, result) {
                    var tr = $(['<tr id="uploadMain-' + index + '">'
                        , '<td>' + file.name + '</td>'
                        , '<td>' + (file.size / 1014).toFixed(1) + 'kb</td>'
                        , '<td>等待上传</td>'
                        , '<td name="goodsMainImgPath"></td>'
                        , '<td>'
                        , '<button class="layui-btn layui-btn-xs demo-reload layui-hide">重传</button>'
                        , '<button class="layui-btn layui-btn-xs layui-btn-danger demo-delete">删除</button>'
                        , '</td>'
                        , '</tr>'].join(''));

                    //单个重传
                    tr.find('.demo-reload').on('click', function () {
                        obj.upload(index, file);
                    });

                    //删除
                    tr.find('.demo-delete').on('click', function () {
                        delete files[index]; //删除对应的文件
                        tr.remove();
                        uploadListMain.config.elem.next()[0].value = ''; //清空 input file 值，以免删除后出现同名文件不可选
                    });

                    imgMainListView.append(tr);
                });
            }
            , done: function (res, index, upload) {
                if (res.status == 0) { //上传成功
                    var tr = imgMainListView.find('tr#uploadMain-' + index)
                            , tds = tr.children();
                    tds.eq(2).html('<span style="color: #5FB878;">上传成功</span>');
                    tds.eq(3).html(res.msg); //清空操作
                    tds.eq(4).html(''); //清空操作
                    return delete this.files[index]; //删除文件队列已经上传成功的文件
                }
                this.error(index, upload);
            }
            , error: function (index, upload) {
                var tr = imgMainListView.find('tr#uploadMain-' + index)
                        , tds = tr.children();
                tds.eq(2).html('<span style="color: #FF5722;">上传失败</span>');
                tds.eq(4).find('.demo-reload').removeClass('layui-hide'); //显示重传
            }
        });
        var imgDetailListView = $('#imgDetailList')
                , uploadListDetail = layui.upload.render({
            elem: '#choseDetail'
            , url: '/util/uploadImg'
            , data: {
                path: 'goods/detail'
            }
            , accept: 'file'
            , multiple: true
            , auto: false
            , bindAction: '#detailAction'
            , choose: function (obj) {
                var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列
                //读取本地文件
                obj.preview(function (index, file, result) {
                    var tr = $(['<tr id="uploadDetail-' + index + '">'
                        , '<td>' + file.name + '</td>'
                        , '<td>' + (file.size / 1014).toFixed(1) + 'kb</td>'
                        , '<td>等待上传</td>'
                        , '<td name="goodsDetailImgPath"></td>'
                        , '<td>'
                        , '<button class="layui-btn layui-btn-xs demo-reload layui-hide">重传</button>'
                        , '<button class="layui-btn layui-btn-xs layui-btn-danger demo-delete">删除</button>'
                        , '</td>'
                        , '</tr>'].join(''));

                    //单个重传
                    tr.find('.demo-reload').on('click', function () {
                        obj.upload(index, file);
                    });

                    //删除
                    tr.find('.demo-delete').on('click', function () {
                        delete files[index]; //删除对应的文件
                        tr.remove();
                        uploadListDetail.config.elem.next()[0].value = ''; //清空 input file 值，以免删除后出现同名文件不可选
                    });
                    imgDetailListView.append(tr);
                });
            }
            , done: function (res, index, upload) {
                if (res.status == 0) { //上传成功
                    var tr = imgDetailListView.find('tr#uploadDetail-' + index)
                            , tds = tr.children();
                    tds.eq(2).html('<span style="color: #5FB878;">上传成功</span>');
                    tds.eq(3).html(res.msg);
                    tds.eq(4).html(''); //清空操作
                    return delete this.files[index]; //删除文件队列已经上传成功的文件
                }
                this.error(index, upload);
            }
            , error: function (index, upload) {
                var tr = imgDetailListView.find('tr#uploadDetail-' + index)
                        , tds = tr.children();
                tds.eq(2).html('<span style="color: #FF5722;">上传失败</span>');
                tds.eq(4).find('.demo-reload').removeClass('layui-hide'); //显示重传
            }
        });
    });
    var addMainTB = function () {
        tbIndex++;
        layer.open({
            content: '<input type="text" id="tbUrl'+tbIndex+'" name="tbUrl'+tbIndex+'" ' +
            'required  lay-verify="required" placeholder="请输入链接" autocomplete="off" class="layui-input">',
            btn: ['确认'],
            yes: function(index, layero){
                layer.close(layer.index);
                var $ = layui.$;
                var url = document.getElementById("tbUrl" + tbIndex).value
                var html = '<tr id="uploadTB-' + tbIndex + '">' +
                        '<td>第三方链接</td><td>未知</td><td>添加成功</td>' +
                        '<td name="goodsMainImgPath">'+url+'</td>' +
                        '<td><button id="uploadTBButton'+tbIndex+'"' +
                        ' class="layui-btn layui-btn-xs layui-btn-danger demo-delete" onclick="deleteTB(this.id)">删除</button></td>' +
                        '</tr>';
                $("#imgMainList").append(html);
            }
        });
    }
    var addDetailTB = function () {
        tbIndex++;
        layer.open({
            content: '<input type="text" id="tbUrl'+tbIndex+'" name="tbUrl'+tbIndex+'" ' +
            'required  lay-verify="required" placeholder="请输入链接" autocomplete="off" class="layui-input">',
            btn: ['确认'],
            yes: function(index, layero){
                layer.close(layer.index);
                var $ = layui.$;
                var url = document.getElementById("tbUrl" + tbIndex).value
                var html = '<tr id="uploadTB-' + tbIndex + '">' +
                        '<td>第三方链接</td><td>未知</td><td>添加成功</td>' +
                        '<td name="goodsDetailImgPath">'+url+'</td>' +
                        '<td><button id="uploadTBButton'+tbIndex+'"' +
                        ' class="layui-btn layui-btn-xs layui-btn-danger demo-delete" onclick="deleteTB(this.id)">删除</button></td>' +
                        '</tr>';
                $("#imgDetailList").append(html);
            }
        });
    }
    var deleteTB = function (obj) {
        var $ = layui.$;
        var index = obj.replace("uploadTBButton","");
        $("#uploadTB-"+index).remove();
    }
    var addGoods = function () {
        var loadIng = layer.load(2);
        var $ = layui.$;
        var styleList = [];
        var name, price, unit, weight, type, material, code, supplierName, supplierUrl, balance, balanceWarn, balanceError, img, note;
        for (var i = 1; i <= index; i++) {
            name = document.getElementById("sname" + i).value;
            price = document.getElementById("price" + i).value;
            unit = document.getElementById("unit" + i).value;
            weight = document.getElementById("weight" + i).value;
            type = document.getElementById("stype" + i).value;
            material = document.getElementById("material" + i).value;
            code = document.getElementById("scode" + i).value;
            supplierName = document.getElementById("supplierName" + i).value;
            supplierUrl = document.getElementById("supplierUrl" + i).value;
            balance = document.getElementById("balance" + i).value;
            balanceWarn = document.getElementById("balanceWarn" + i).value;
            balanceError = document.getElementById("balanceError" + i).value;
            img = document.getElementById("goodsStyleValue" + i).value;
            note = document.getElementById("note" + i).value;
            if (name == '') {
                layer.close(loadIng);
                layer.msg('款式名称不能为空');
                return;
            }
            if (price == '') {
                layer.close(loadIng);
                layer.msg('款式单价不能为空');
                return;
            }
            if (unit == '') {
                layer.close(loadIng);
                layer.msg('款式单位不能为空');
                return;
            }
            if (weight == '') {
                layer.close(loadIng);
                layer.msg('款式重量不能为空');
                return;
            }
            if (code == '') {
                layer.close(loadIng);
                layer.msg('款式编号不能为空');
                return;
            }
            if (balance == '') {
                layer.close(loadIng);
                layer.msg('款式剩余库存不能为空');
                return;
            }
            if (balanceWarn == '') {
                layer.close(loadIng);
                layer.msg('款式黄色预警值不能为空');
                return;
            }
            if (balanceError == '') {
                layer.close(loadIng);
                layer.msg('款式红色预警值不能为空');
                return;
            }
            if (img == '') {
                layer.close(loadIng);
                layer.msg('款式图片不能为空');
                return;
            }
            var styleModel = {
                name: name,
                price: price,
                unit: unit,
                weight: weight,
                type: type,
                material: material,
                code: code,
                supplierName: supplierName,
                supplierUrl: supplierUrl,
                balance: balance,
                balanceWarn: balanceWarn,
                balanceError: balanceError,
                imgPath: img,
                note: note
            }
            styleList.push(styleModel)
        }
        name = document.getElementById("name").value;
        code = document.getElementById("code").value;
        price = document.getElementById("price").value;
        var mainImg = getMainImgList();
        var detailImg = getDetailImgList();
        if (name == '') {
            layer.close(loadIng);
            layer.msg('商品名称不能为空');
            return;
        }
        if (code == '') {
            layer.close(loadIng);
            layer.msg('商品编号不能为空');
            return;
        }
        if (price == '') {
            layer.close(loadIng);
            layer.msg('一口价不能为空');
            return;
        }
        var baseGoods = {
            name: name,
            typeId: document.getElementById("type").value,
            status: document.getElementById("status").value,
            code: code,
            price: price,
        }
        var model = {
            baseGoods: baseGoods,
            goodsStyles: styleList,
            mainImgPath: mainImg,
            detailImgPath: detailImg
        }

        $.ajax({
            type: "POST",
            url: "/goods/addGoods",
            data: JSON.stringify(model),
            dataType: "json",
            contentType: 'application/json',
            success: function (data) {
                if (data.status != 0) {
                    layer.close(loadIng);
                    layer.open({
                        title: '提示'
                        , content: data.msg
                    });
                } else {

                    window.location.href = '../templates/goodsManage.html'
                }
            },
            error: function () {
                layer.close(loadIng);
            }
        });
    }
    var getMainImgList = function () {
        var $ = layui.$;
        var domList = $("#imgMainList").find("tr>td[name=goodsMainImgPath]");
        var list = [];
        for (var i = 0; i < domList.length; i++) {
            list.push(domList.eq(i).text());
        }
        return list;
    };
    var getDetailImgList = function () {
        var $ = layui.$;
        var domList = $("#imgDetailList").find("tr>td[name=goodsDetailImgPath]");
        var list = [];
        for (var i = 0; i < domList.length; i++) {
            list.push(domList.eq(i).text());
        }
        return list;
    };
    var addStyleModel = function () {
        index++;
        var $ = layui.$;
        var html = getHtmlContent(index);
        $("#modelList").append(html);
        layui.upload.render({
            elem: "#goodsStyleImg" + index
            , url: '/util/uploadImg'
            , data: {
                path: 'goods/goodsStyleImg',
                type: 'reduce'
            }
            , done: function (res) {
                var inde = this.item[0].id.replace('goodsStyleImg', '')
                console.log(inde);
                $("#goodsStyleContent" + inde).html("上传成功");
                $("#goodsStyleValue" + inde).val(res.msg);
            }
            , acceptMime: 'image/*'
        });
    };
    var getHtmlContent = function (index) {
        var value = {};
        if (index > 1) {
            value = {
                sname: 'value="'+document.getElementById("sname" + (index-1)).value+'"',
                price: 'value="'+document.getElementById("price" + (index-1)).value+'"',
                unit: 'value="'+document.getElementById("unit" + (index-1)).value+'"',
                weight: 'value="'+document.getElementById("weight" + (index-1)).value+'"',
                stype: 'value="'+document.getElementById("stype" + (index-1)).value+'"',
                material: 'value="'+document.getElementById("material" + (index-1)).value+'"',
                scode: 'value="'+document.getElementById("code").value+'-'+index+'"',
                balance: 'value="'+document.getElementById("balance" + (index-1)).value+'"',
                supplierName: 'value="'+document.getElementById("supplierName" + (index-1)).value+'"',
                supplierUrl: 'value="'+document.getElementById("supplierUrl" + (index-1)).value+'"',
                balanceWarn: 'value="'+document.getElementById("balanceWarn" + (index-1)).value+'"',
                balanceError: 'value="'+document.getElementById("balanceError" + (index-1)).value+'"',
                note: 'value="'+document.getElementById("note" + (index-1)).value+'"',
            }
        }else{
            value = {
                sname: "",
                price: "",
                unit: "",
                weight: "",
                stype: "",
                material: "",
                scode: "",
                balance: "",
                supplierName: "",
                supplierUrl: "",
                balanceWarn: "",
                balanceError: "",
                note: "",
            }
        }
        return '<div class="layui-form-item">' +
                '<div class="layui-inline">' +
                '<label class="layui-form-label">款式名称</label>' +
                '<div class="layui-input-block">' +
                '<input type="text" id="sname' + index + '" name="sname' + index + '" autocomplete="off" placeholder="请输入标题"' +
                'class="layui-input" '+value.sname+'>' +
                '</div>' +
                '</div>' +
                '<div class="layui-inline">' +
                '<label class="layui-form-label">单价</label>' +
                '<div class="layui-input-block">' +
                '<input type="text" id="price' + index + '" name="price' + index + '" autocomplete="off" placeholder="请输入单价"' +
                'class="layui-input" '+value.price+'>' +
                '</div>' +
                '</div>' +
                '<div class="layui-inline">' +
                '<label class="layui-form-label">单位</label>' +
                '<div class="layui-input-block">' +
                '<input type="text" id="unit' + index + '" name="unit' + index + '" autocomplete="off" placeholder="请输入该单价下计量单位"' +
                'class="layui-input"  '+value.unit+'>' +
                '</div>' +
                '</div>' +
                '<div class="layui-inline">' +
                '<label class="layui-form-label">重量</label>' +
                '<div class="layui-input-block">' +
                '<input type="text" id="weight' + index + '" name="weight' + index + '" autocomplete="off"' +
                'placeholder="重量单位(kg)"' +
                'class="layui-input"  '+value.weight+'>' +
                '</div>' +
                '</div>' +
                '</div>' +
                '<div class="layui-form-item">' +
                '<div class="layui-inline">' +
                '<label class="layui-form-label">类型</label>' +
                '<div class="layui-input-block">' +
                '<input type="text" id="stype' + index + '" name="stype' + index + '" autocomplete="off" placeholder="请输入类型"' +
                'class="layui-input"  '+value.stype+'>' +
                '</div>' +
                '</div>' +
                '<div class="layui-inline">' +
                '<label class="layui-form-label">材质</label>' +
                '<div class="layui-input-block">' +
                '<input type="text" id="material' + index + '" name="material' + index + '" autocomplete="off"' +
                'placeholder="请输入材质"' +
                'class="layui-input"  '+value.material+'>' +
                '</div>' +
                '</div>' +
                '<div class="layui-inline">' +
                '<label class="layui-form-label">编号</label>' +
                '<div class="layui-input-block">' +
                '<input type="text" id="scode' + index + '" name="scode' + index + '" autocomplete="off"' +
                'placeholder="编号将用于微信扫码"' +
                'class="layui-input"  '+value.scode+'>' +
                '</div>' +
                '</div>' +
                '<div class="layui-inline">' +
                '<label class="layui-form-label">剩余库存</label>' +
                '<div class="layui-input-block">' +
                '<input type="text" id="balance' + index + '" name="balance' + index + '" autocomplete="off"' +
                'placeholder="剩余库存即总库存"' +
                'class="layui-input"  '+value.balance+'>' +
                '</div>' +
                '</div>' +
                '</div>' +
                '<div class="layui-form-item">' +
                '<div class="layui-inline">' +
                '<label class="layui-form-label">供应商</label>' +
                '<div class="layui-input-block">' +
                '<input type="text" id="supplierName' + index + '" name="supplierName' + index + '" autocomplete="off"' +
                'placeholder="请输入供应商"' +
                'class="layui-input"  '+value.supplierName+'>' +
                '</div>' +
                '</div>' +
                '<div class="layui-inline">' +
                '<label class="layui-form-label">供应商地址</label>' +
                '<div class="layui-input-block">' +
                '<input type="text" id="supplierUrl' + index + '" name="supplierUrl' + index + '" autocomplete="off"' +
                'placeholder="供应商地址"' +
                'class="layui-input"  '+value.supplierUrl+'>' +
                '</div>' +
                '</div>' +
                '</div>' +
                '<div class="layui-form-item">' +
                '<div class="layui-inline">' +
                '<label class="layui-form-label">黄色预警</label>' +
                '<div class="layui-input-block">' +
                '<input type="text" id="balanceWarn' + index + '" name="balanceWarn' + index + '" autocomplete="off"' +
                'placeholder="请输入预警阈值"' +
                'class="layui-input"  '+value.balanceWarn+'>' +
                '</div>' +
                '</div>' +
                '<div class="layui-inline">' +
                '<label class="layui-form-label">红色预警</label>' +
                '<div class="layui-input-block">' +
                '<input type="text" id="balanceError' + index + '" name="balanceWarn' + index + '" autocomplete="off"' +
                'placeholder="请输入预警阈值"' +
                'class="layui-input"  '+value.balanceError+'>' +
                '</div>' +
                '</div>' +
                '<div class="layui-upload-drag" id="goodsStyleImg' + index + '" name="imgUpload">' +
                '<i class="layui-icon"></i>' +
                '<p id="goodsStyleContent' + index + '">点击上传，或直接输入图片链接</p>' +
                '<input type="text" id="goodsStyleValue' + index + '" ></input>' +
                '</div>' +
                '</div>' +
                '<label class="layui-form-label">备注</label>' +
                '<div class="layui-input-block">' +
                '<input type="text" id="note' + index + '" name="note' + index + '" autocomplete="off"' +
                'placeholder="备注"' +
                'class="layui-input"  '+value.note+'>' +
                '</div>' + '<br>' +
                '<fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">' +
                '<legend>以上为第' + index + '个款式</legend>' +
                '</fieldset><br>'
    }
</script>
</body>
</html>